package com.painarthur.rbac.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.painarthur.rbac.dao.TreeNodeDAO;
import com.painarthur.rbac.po.ZtreePO;
import com.test.bean.Student;
import com.test.service.StudentService;

@ParentPackage("default")
@Namespace(value = "/")
@Result(location = "/ztree.jsp")
@Results({ @Result(name = "successTree", location = "/ztree.jsp", type = "json", params = {
		"noCache", "true", "contentType", "text/json", "root", "result" }), })
public class ZTreeAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private JSONArray jsonArray = null;
	private List<ZtreePO> list = new ArrayList<ZtreePO>();
	private String result;

	@Action(value = "doLoadNode")
	public String doLoadNode() throws Exception {
		TreeNodeDAO tdn = new TreeNodeDAO();
		list = tdn.getTreeAllNodes();
		jsonArray = JSONArray.fromObject(list);
		//JSONObject jo = (JSONObject) JSONObject.fromObject(list);
		this.result = jsonArray.toString();
		System.out.println(this.result);
		return "successTree";
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public List<ZtreePO> getList() {
		return list;
	}

	public void setList(List<ZtreePO> list) {
		this.list = list;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public JSONArray getJsonArray() {
		return jsonArray;
	}

	public void setJsonArray(JSONArray jsonArray) {
		this.jsonArray = jsonArray;
	}

	public void doTradition() {
		TreeNodeDAO tdn = new TreeNodeDAO();
		list = tdn.getMakeNodes();
		jsonArray = JSONArray.fromObject(list);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=gbk");
		PrintWriter out = null;
		try {
			out = response.getWriter();
			out.println(jsonArray.toString());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			out.flush();
			out.close();
		}
	}
}
